package top.xzxsrq.fileviewutils.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.xzxsrq.fileviewutils.entity.ShareFile;
import top.xzxsrq.fileviewutils.entity.ShareFileSearch;
import top.xzxsrq.fileviewutils.entity.ShareFileWithUser;
import top.xzxsrq.fileviewutils.service.FileService;
import top.xzxsrq.web.utils.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("getFileList")
    public AjaxResult getFileList(String basePath) {
        return fileService.getFileList(basePath);
    }

    @PostMapping("getDirFileList")
    public AjaxResult getDirFileList(String basePath) {
        return fileService.getDirFileList(basePath);
    }

    /**
     * 接受filePath
     * @param request
     * @param response
     */
    @GetMapping("getFileRange/**")
    public void getFileRange(HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.getFileRange(request,response);
    }

    @GetMapping("getFile/**")
    public void getFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.getFile(request,response);
    }

    @GetMapping("getFileIcon/**")
    public void getFileIcon(HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.getFileIcon(request,response);
    }

    @PostMapping("getTextFile")
    public AjaxResult getTextFile(String filePath) throws IOException {
        return fileService.getTextFile(filePath);
    }

    @PostMapping("saveTextFile")
    @PreAuthorize("hasAnyAuthority('edit')")
    public AjaxResult saveTextFile(String filePath, String content) throws IOException {
        return fileService.saveTextFile(filePath,content);
    }

    @PostMapping("rename")
    @PreAuthorize("hasAnyAuthority('rename')")
    public AjaxResult rename(String filePath, String newName) throws IOException {
        return fileService.rename(filePath,newName);
    }

    @PostMapping("deleteFileOrDir")
    @PreAuthorize("hasAnyAuthority('del')")
    public AjaxResult deleteFileOrDir(@RequestBody List<String> filePaths) throws IOException {
        return fileService.deleteFileOrDir(filePaths);
    }

    @PostMapping("copyFileOrDir")
    @PreAuthorize("hasAnyAuthority('copy')")
    public AjaxResult copyFileOrDir(@RequestBody List<String> filePaths, String targetPath) throws IOException {
        return fileService.copyFileOrDir(filePaths,targetPath);
    }

    @PostMapping("removeFileOrDir")
    @PreAuthorize("hasAnyAuthority('cut')")
    public AjaxResult removeFileOrDir(@RequestBody List<String> filePaths, String targetPath) throws IOException {
        return fileService.removeFileOrDir(filePaths,targetPath);
    }

    /**
     * 是否存在同名文件 1是 0否
     */
    @PostMapping("hasFile")
    public AjaxResult hasFile(String basePath) throws IOException {
        return fileService.hasFile(basePath);
    }

    /**
     * 使用@RequestPart注解代替了@RequestParam，以支持文件的流式处理
     * @param basePath
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("uploaderFile")
    @PreAuthorize("hasAnyAuthority('upload')")
    public AjaxResult uploaderFile(String basePath, @RequestPart MultipartFile file) throws IOException {
        return fileService.uploaderFile(basePath,file);
    }

    @PostMapping("shareFileOrDir")
    @PreAuthorize("hasAnyAuthority('share')")
    public AjaxResult shareFileOrDir(@RequestBody List<ShareFile> shareFiles) throws IOException {
        return fileService.shareFileOrDir(shareFiles);
    }

    @PostMapping("shareFileList")
    public AjaxResult shareFileList(@RequestBody ShareFileSearch shareFile, int pageNum, int pageSize) {
        return fileService.shareFileList(shareFile,pageNum,pageSize);
    }

    @PostMapping("shareFileDelete")
    public AjaxResult shareFileDelete(@RequestBody List<String> shareFileDeletes) {
        return fileService.shareFileDelete(shareFileDeletes);
    }

    @PostMapping("shareFileUpdate")
    public AjaxResult shareFileUpdate(@RequestBody ShareFile shareFile) {
        return fileService.shareFileUpdate(shareFile);
    }

    @PostMapping("getFileInfo")
    public AjaxResult getFileInfo(String basePath) throws IOException {
        return fileService.getFileInfo(basePath);
    }
    @PostMapping("createFile")
    @PreAuthorize("hasAnyAuthority('mkFile')")
    public AjaxResult createFile(String basePath) throws IOException {
        return fileService.createFile(basePath);
    }
    @PostMapping("createDir")
    @PreAuthorize("hasAnyAuthority('mkdir')")
    public AjaxResult createDir(String basePath) throws IOException {
        return fileService.createDir(basePath);
    }
    @PostMapping("downloadFileOrDir")
    public AjaxResult downloadFileOrDir(String basePath,@RequestBody List<String> filePaths) throws IOException {
        return fileService.downloadFileOrDir(basePath,filePaths);
    }

    @GetMapping("downloadFileOrDirById")
    public void downloadFileOrDirById(String downLoadId,HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.downloadFileOrDirById(downLoadId, request, response);
    }
    @PostMapping("getDiskInfo")
    public AjaxResult getDiskInfo() throws IOException {
        return fileService.getDiskInfo();
    }

    @PostMapping("getShareFile")
    public AjaxResult getShareFile(String id) {
        return fileService.getShareFile(id);
    }

    @PostMapping("shareFileDown")
    public AjaxResult shareFileDown(@RequestBody ShareFileWithUser shareFileWithUser) throws IOException {
        return fileService.shareFileDown(shareFileWithUser);
    }

}
